package com.lpf.www.common.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.SetParams;
import redis.clients.jedis.params.ZAddParams;

/**
 * @Author lpf
 * @Date 2022/1/4 下午10:08
 * @ProjectName live-streaming-os
 * @Version 1.0
 * @Description: 验证码处理
 */
public class VerificationCode {

    private static final Jedis jedis = JedisUtils.getJedis();

    /**
     * 生成验证码并保存在redis中，然后设置一个5分钟的过期时间，等待用户输入
     *
     * @return
     */
    private int getVerificationCode() {
        int res = (int) ((Math.random() * 9 + 1) * 100000);
        return res;
    }

    public static boolean saveCode(String phone) {
        int code = new VerificationCode().getVerificationCode();
        System.out.println(code);
        SetParams params = new SetParams();
        params.ex(600l);
        String set = jedis.set(phone, code + "", params);
        return "OK".equals(set);
    }

    public static String getCode(String phone) {
        return jedis.get(phone);
    }

    public static Long getCodeTTL(String phone) {
        return jedis.ttl(phone);
    }

    public static void delCode(String phone) {
        jedis.del(phone);
    }

}
